import os
import sqlite3
from app import create_app

# 2025年最新战队数据
TEAMS_DATA_2025 = [
    {"name": "T1", "league": "LCK", "game": "英雄联盟"},
    {"name": "Hanwha Life Esports", "league": "LCK", "game": "英雄联盟"},
    {"name": "OKSavingsBank BRION", "league": "LCK", "game": "英雄联盟"},
    {"name": "DN Freecs", "league": "LCK", "game": "英雄联盟"},
    {"name": "BNK FEARX", "league": "LCK", "game": "英雄联盟"},
    {"name": "Dplus KIA", "league": "LCK", "game": "英雄联盟"},
    {"name": "Gen.G", "league": "LCK", "game": "英雄联盟"},
    {"name": "KT Rolster", "league": "LCK", "game": "英雄联盟"},
    {"name": "DRX", "league": "LCK", "game": "英雄联盟"},
    {"name": "Nongshim RedForce", "league": "LCK", "game": "英雄联盟"},
    
    {"name": "Anyone's Legend", "league": "LPL", "game": "英雄联盟"},
    {"name": "Bilibili Gaming", "league": "LPL", "game": "英雄联盟"},
    {"name": "EDward Gaming", "league": "LPL", "game": "英雄联盟"},
    {"name": "FunPlus Phoenix", "league": "LPL", "game": "英雄联盟"},
    {"name": "Invictus Gaming", "league": "LPL", "game": "英雄联盟"},
    {"name": "JD Gaming", "league": "LPL", "game": "英雄联盟"},
    {"name": "LGD Gaming", "league": "LPL", "game": "英雄联盟"},
    {"name": "LNG Esports", "league": "LPL", "game": "英雄联盟"},
    {"name": "Ninjas in Pyjamas", "league": "LPL", "game": "英雄联盟"},
    {"name": "Oh My God", "league": "LPL", "game": "英雄联盟"},
    {"name": "Royal Never Give Up", "league": "LPL", "game": "英雄联盟"},
    {"name": "Team WE", "league": "LPL", "game": "英雄联盟"},
    {"name": "ThunderTalk Gaming", "league": "LPL", "game": "英雄联盟"},
    {"name": "Top Esports", "league": "LPL", "game": "英雄联盟"},
    {"name": "Ultra Prime", "league": "LPL", "game": "英雄联盟"},
    {"name": "Weibo Gaming", "league": "LPL", "game": "英雄联盟"},
    
    {"name": "Fnatic", "league": "LEC", "game": "英雄联盟"},
    {"name": "Karmine Corp", "league": "LEC", "game": "英雄联盟"},
    {"name": "G2 Esports", "league": "LEC", "game": "英雄联盟"},
    {"name": "Movistar KOI", "league": "LEC", "game": "英雄联盟"},
    {"name": "Team Vitality", "league": "LEC", "game": "英雄联盟"},
    {"name": "GIANTX", "league": "LEC", "game": "英雄联盟"},
    {"name": "Team BDS", "league": "LEC", "game": "英雄联盟"},
    {"name": "Team Heretics", "league": "LEC", "game": "英雄联盟"},
    {"name": "SK Gaming", "league": "LEC", "game": "英雄联盟"},
    {"name": "Rogue", "league": "LEC", "game": "英雄联盟"}
]

def update_teams_directly():
    """
    直接使用SQLite更新战队数据
    """
    app = create_app()
    
    with app.app_context():
        db_path = os.path.join(app.instance_path, 'esports.db')
        if not os.path.exists(db_path):
            print("数据库文件不存在")
            return
        
        # 使用SQLite直接连接
        conn = sqlite3.connect(db_path)
        cursor = conn.cursor()
        
        try:
            # 获取当前有哪些战队
            cursor.execute("SELECT name FROM teams")
            existing_teams = [row[0] for row in cursor.fetchall()]
            print(f"当前数据库中有 {len(existing_teams)} 支战队")
            
            # 清空战队表，重新创建
            print("清空战队表")
            cursor.execute("DELETE FROM teams")
            
            # 添加新战队数据
            for team_data in TEAMS_DATA_2025:
                try:
                    cursor.execute(
                        "INSERT INTO teams (name, league, game, icon, total_matches, wins) VALUES (?, ?, ?, ?, 0, 0)",
                        (team_data["name"], team_data["league"], team_data["game"], "default_team.png")
                    )
                    print(f"添加战队: {team_data['name']}")
                except Exception as e:
                    print(f"添加战队 {team_data['name']} 失败: {str(e)}")
            
            # 提交事务
            conn.commit()
            
            # 检查添加了多少战队
            cursor.execute("SELECT COUNT(*) FROM teams")
            count = cursor.fetchone()[0]
            print(f"成功添加了 {count} 支战队")
            
        except Exception as e:
            conn.rollback()
            print(f"更新战队失败: {str(e)}")
        finally:
            conn.close()

if __name__ == "__main__":
    update_teams_directly() 